﻿using UnityEngine;
using System.Collections;

public class AbiliMove : Ability
{
    

    PlayerModel player;
    bool IsSelfUnit = false;
    bool IsMoveing;
    Vector3 TargetPos;
    public override void Init(Unit unit)
    {
        base.Init(unit);
        player = Game.Model.Get<PlayerModel>();
        this.IsMoveing = false;
        
        if (unit.Id == player.UserName) 
        {
            IsSelfUnit = true;
            Game.Event.AddHandler("SceneClick", OnSceneClick);
        }
    }

    public override void OnLoad()
    {
 
    }

    

    public void OnSceneClick(object param0, object param1)
    {
        if (this.mUnit.IsDie) 
        {
            return;
        }
        Vector3 v3 = (Vector3)param0;
        if (Vector3.Distance(v3, this.mUnit.transform.localPosition) < 1f) 
        {
            return;
        }
        this.TargetPos = new Vector3(v3.x, v3.y, v3.z);
        this.mUnit.CmdQueue.Clear();
        BaseCommand cmd = this.mUnit.CmdQueue.Push(CommandType.Unit_Move,v3.x,v3.y,v3.z);

        Game.Packet.SendPushUnitCommand(this.mUnit.Id, CommandType.Unit_Move, PacketHelper.PushCmdUnitType.ClearPush, cmd.Args);
    }

    public void ServerSynPosition(Vector3 v3,bool isMove)
    {
        if (isMove==false)
        {
            this.mUnit.CallEvent("Stand");
            this.mUnit.transform.localPosition = v3;
            this.IsMoveing = false;
            return;
        }

        if (this.IsMoveing == false) 
        {
            this.mUnit.CallEvent("Move");
            this.IsMoveing = true;
        }
        this.mUnit.transform.LookAt(v3);
        this.mUnit.transform.localPosition = v3;
    }

   

   
}
